//
//  BMAnnotationView.m
//  BicycleMap
//
//  Created by vedon on 5/18/16.
//  Copyright © 2016 Qianqian. All rights reserved.
//

#import "BMAnnotationView.h"
#import "UIImageView+WebCache.h"
#import "BMBicycleAnnotationCalloutView.h"
#import "BMPointAnnotation.h"

@interface BMAnnotationView ()<BMBicycleAnnotationCalloutViewDelegate>
@property (strong,nonatomic) UIImageView *avatarImageView;
@property (strong,nonatomic) BMBicycleAnnotationCalloutView *calloutView;
@end

@implementation BMAnnotationView

- (instancetype)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier avatarUrl:(NSString *)avatarUrl
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self addSubview:self.avatarImageView];
        self.paopaoView = [[BMKActionPaopaoView alloc] initWithCustomView:self.calloutView];
        self.calloutOffset = CGPointMake(0, -10);
        
        [self configureWithAnnotation:annotation];
    
    }
    return  self;
}

- (void)configureWithAnnotation:(BMPointAnnotation *)annotation
{
    self.annotation = annotation;
    
    //下载头像
    [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:[annotation.userInfo avatarUrl]] placeholderImage:[UIImage imageNamed:@"annotationViewDefaultImage"]];
    
    [self.calloutView configureWithData:annotation];
}

#pragma mark - BMBicycleAnnotationCalloutViewDelegate

- (void)didTapNavButton
{
    if ([self.delegate respondsToSelector:@selector(didTapNavButton:)])
    {
        [self.delegate didTapNavButton:self.annotation.coordinate];
    }
}

#pragma mark - Getter & Setter

- (UIImageView *)avatarImageView
{
    if (!_avatarImageView)
    {
        _avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        _avatarImageView.contentMode = UIViewContentModeScaleAspectFit;
        _avatarImageView.layer.cornerRadius = CGRectGetWidth(_avatarImageView.frame)/2;
        _avatarImageView.layer.masksToBounds = YES;
        
        
//        _avatarImageView.userInteractionEnabled = YES;
//        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAnnotationView)];
//        [_avatarImageView addGestureRecognizer:tapGesture];
//        tapGesture = nil;
    }
    
    return _avatarImageView;
}


- (BMBicycleAnnotationCalloutView *)calloutView
{
    if (!_calloutView)
    {
        _calloutView = [[BMBicycleAnnotationCalloutView alloc] init];
        _calloutView.frame = CGRectMake(0, 0, 160, [BMBicycleAnnotationCalloutView mimimumHeight]);
        _calloutView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        _calloutView.layer.cornerRadius = 3.0;
        _calloutView.layer.masksToBounds = YES;
        _calloutView.delegate = self;
    }
    return _calloutView;
}

@end
